package com.fjbdxk.beidou.bd;

import com.alibaba.fastjson.JSONObject;
import com.fjbdxk.beidou.SendExampleTest;
import com.fjbdxk.beidou.core.ByteUtils;
import com.fjbdxk.beidou.mysql.DatabaseHelper;
import com.fjbdxk.beidou.mysql.WindTurbineData;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

public class RabbitMQExample {

    public final static int part = 20;//每段20字节，增加的话修改20的倍数

    public static void main(String[] argv) throws Exception {

        // 创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("101.204.230.131"); // RabbitMQ服务器地址
        factory.setPort(8909);
        factory.setUsername("bdUser");
        factory.setPassword("admin123456");
        factory.setVirtualHost("bd");

        // 创建连接
        Connection connection = factory.newConnection();
        // 创建通道
        Channel channel = connection.createChannel();

        // 开始消费消息
        channel.basicConsume("bd_message_queue", true,
                (consumerTag, delivery) -> {
                    // 当消息送达时的回调函数
                    String message = new String(delivery.getBody(), "UTF-8"); // 将收到的消息内容转换为字符串
                    JSONObject parse = JSONObject.parseObject(message); // 解析JSON格式的消息内容
                    String messageData = parse.getString("messageData"); // 从解析后的JSON中获取消息数据
                    System.out.println(" [x] Received '" + messageData + "'"); // 输出接收到的消息数据
                    //getMsg(messageData); // 处理消息数据的方法，需要根据具体逻辑实现
                    getPart(messageData); // 处理消息数据的方法，需要根据具体逻辑实现
                },
                consumerTag -> {
                    // 当消费者取消时的回调函数
                });
    }


    public static void getPart(String msg){
        // 将输入的十六进制字符串转换为字节数组
        byte[] pack = ByteUtils.hexStr2bytes(msg);
        // 解析待发送报文，先申请一个报文解析对象
        BeidouPacketL2 packetL2 = new BeidouPacketL2();
        packetL2.parsePacket(pack);
        PacketData packetData = packetL2.packetData;
        PacketData.DeviceDataList deviceDataList = packetData.deviceDataList;

        for (PacketData.DeviceSet deviceSet : deviceDataList.deviceSet) {

            String time = getTime();// 获取当前时间

            int i = 0;
            String l1 = "";
            String l2 = "";
            String l3 = "";
            String l4 = "";
            String l5 = "";
            String l6 = "";
            String l7 = "";
            String l8 = "";
            String l9 = "";
            String l10 = "";

            for (DeviceData deviceData : deviceSet.dataArray) {
                if(i==0){
                    double constant = deviceData.tagConfig.constant;
                    l1 = constant+"";
                    System.out.println("PLC状态："+l1);
                }
                if(i==1){
                    double constant = deviceData.tagConfig.constant;
                    l2 = constant+"";

                    System.out.println("环境温度："+l2);
                }
                if(i==2){
                    double constant = deviceData.tagConfig.constant;
                    l3 = constant+"";
                    System.out.println("风速："+l3);
                }
                if(i==3){
                    double constant = deviceData.tagConfig.constant;
                    l4 = constant+"";
                    System.out.println("功率："+l4);
                }
                if(i==4){
                    double constant = deviceData.tagConfig.constant;
                    l5 = constant+"";
                    System.out.println("转矩%："+l5);
                }
                if(i==5){
                    double constant = deviceData.tagConfig.constant;
                    l6 = constant+"";
                    System.out.println("发电机转速："+l6);
                }
                if(i==6){
                    double constant = deviceData.tagConfig.constant;
                    l7 = constant+"";
                    System.out.println("叶轮转速："+l7);
                }
                if(i==7){
                    double constant = deviceData.tagConfig.constant;
                    l8 = constant+"";
                    System.out.println("风向："+l8);
                }
                if(i==8){
                    double constant = deviceData.tagConfig.constant;
                    l9 = constant+"";
                    System.out.println("桨聚角："+l9);
                }
                if(i==9){
                    double constant = deviceData.tagConfig.constant;
                    l10 = constant+"";
                    System.out.println("发电量："+l10);
                }

                i++;

            }
            //创建一个WindTurbineData对象，传递getTime()方法的返回值和十个intValue变量作为参数
            WindTurbineData data = new WindTurbineData(time, l1, l2, l3, l4, l5, l6, l7, l8, l9, l10);
            // 调用DatabaseHelper的insertWindTurbineData方法，将data作为参数，将返回的布尔值赋给success变量
            boolean success = DatabaseHelper.insertWindTurbineData(data);
            if (success) {
                System.out.println("数据入库成功！");
            } else {
                System.out.println("数据入库失败！");
            }
            //break;




        }

    }




    public static void getMsg(String message,String time){
        //byte[] bytes = ByteUtils.hexStr2bytes(message);
        //String content = new String(bytes);
        String content = message;
        //获取PLC状态
        System.out.println(content);
        // 将二进制字符串转换为整数
        String substring = content.substring(0, 8);
        String intValue = getint(substring,1.0);
        System.out.println("PLC状态："+intValue);
        // 环境温度,将二进制字符串转换为整数
        String substring1 = content.substring(8, 16);
        String intValue1 = getint(substring1,1.0);
        System.out.println("环境温度："+intValue1);

        // 风速,将二进制字符串转换为整数
        String substring2 = content.substring(16, 16+8*2);
        String intValue2 = getint(substring2,0.01);
        System.out.println("风速："+intValue2);

        // 功率,将二进制字符串转换为整数
        String substring3 = content.substring(16+8*2, 16+8*4);
        String intValue3 = getint(substring3,0.01);
        System.out.println("功率："+intValue3);

        // 转矩%,将二进制字符串转换为整数
        String substring4 = content.substring(16+8*4, 16+8*6);
        String intValue4 = getint(substring4,0.1);
        System.out.println("转矩%："+intValue4);

        // 发电机转速,将二进制字符串转换为整数
        String substring5 = content.substring(16+8*6, 16+8*8);
        String intValue5 = getint(substring5,1.0);
        System.out.println("发电机转速："+intValue5);

        // 叶轮转速,将二进制字符串转换为整数
        String substring6 = content.substring(16+8*8, 16+8*10);
        String intValue6 = getint(substring6,0.01);
        System.out.println("叶轮转速："+intValue6);

        // 风向,将二进制字符串转换为整数
        String substring7 = content.substring(16+8*10, 16+8*12);
        String intValue7 = getint(substring7,0.1);
        System.out.println("风向："+intValue7);

        // 桨聚角,将二进制字符串转换为整数
        String substring8 = content.substring(16+8*12, 16+8*14);
        String intValue8 = getint(substring8,0.1);
        System.out.println("桨聚角："+intValue8);

        // 发电量,将二进制字符串转换为整数
        String substring9 = content.substring(16+8*14, 16+8*18);
        String intValue9= getint(substring9,1.0);
        System.out.println("发电量："+intValue9);
        //创建一个WindTurbineData对象，传递getTime()方法的返回值和十个intValue变量作为参数
        WindTurbineData data = new WindTurbineData(time, intValue, intValue1, intValue2, intValue3, intValue4, intValue5, intValue6, intValue7, intValue8, intValue9);
        // 调用DatabaseHelper的insertWindTurbineData方法，将data作为参数，将返回的布尔值赋给success变量
        boolean success = DatabaseHelper.insertWindTurbineData(data);
        if (success) {
            System.out.println("数据入库成功！");
        } else {
            System.out.println("数据入库失败！");
        }
    }
    // 定义一个公共静态方法，返回类型为String
    public static String getTime(){
        // 创建一个SimpleDateFormat对象，指定日期格式为"yyyy-MM-dd HH:mm:ss"
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 使用当前时间创建一个Date对象，并将其格式化为字符串
        String formattedDate = sdf.format(new Date());
        // 打印当前时间
        System.out.println("当前时间: " + formattedDate);
        // 返回格式化后的时间字符串
        return formattedDate;
    }


    // 定义一个公共静态方法，接收一个字符串和一个double类型的参数，返回一个字符串
    public static String getint(String content, double xi){
        // 如果content包含"#"，返回"error"
        if(content.contains("#")){
            return "error";
        }
        // 将content按二进制解析成整数intValue
        int intValue = Integer.parseInt(content, 2);
        // 将intValue乘以xi得到结果v
        double v = intValue * xi;
        // 将v转换为整数并返回对应的字符串形式
        return (int) v + "";
    }

    //byte转为short
//    public static String toData1(short number){
//        // 十六进制字符串表示的字节数据
//        String hexString = "0032";
//
//        // 解析十六进制字符串为字节数组
//        byte[] bytes = hexStringToByteArray(hexString);
//
//        // 将字节数组转换为 short 类型
//        short value = bytesToShort(bytes);
//
//        // 输出转换后的 short 值
//        System.out.println("转换后的 short 值: " + value);
//        return "";
//    }
    // 辅助方法：将十六进制字符串转换为字节数组
    private static byte[] hexStringToByteArray(String hexString) {
        int len = hexString.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
                    + Character.digit(hexString.charAt(i+1), 16));
        }
        return data;
    }
    private static short bytesToShort(byte[] bytes) {
        return (short) (((bytes[0] & 0xFF) << 8) | (bytes[1] & 0xFF));
    }
}
